﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DLULearningNetworkModel
{
    public partial class CourseNotification
    {
        public static List<CourseNotification> GetNewCourseNotification(Guid accountID, int skip, int take)
        {
            try
            {
                using (DLULNEntities db = new DLULNEntities())
                {
                    return db.CourseNotifications.Include("Feed").Include("Feed.Account")
                        .Include("Feed.Account1").Include("Feed.FeedComment.Feed")
                        .Include("Feed.FeedComment.Feed.Account")
                        .Include("Feed.FeedComment.Feed.Account1")
                        .Include("Feed.FeedComment.Feed.Course")
                        .Include("Feed.Course")
                        .Where(cNotification => cNotification.AccountID_u == accountID && cNotification.IsRead_b == false)
                        .OrderByDescending(cNotification => cNotification.Feed.CreateDate_dt).Skip(skip).Take(take).ToList();
                }
            }
            catch
            {
                return null;
            }
        }

        public static List<CourseNotification> GetPager(IQueryable<CourseNotification> courseNotifications, int page, int numberOfItemPerPage, ref int totalItems)
        {
            totalItems = courseNotifications.Count();
            if (page < 1)
            {
                page = 1;
            }

            return courseNotifications.OrderByDescending(cn => cn.Feed.CreateDate_dt).OrderBy(cn=>cn.IsRead_b).Skip((page - 1) * numberOfItemPerPage).Take(numberOfItemPerPage).ToList();
        }

        public static List<CourseNotification> GetAllCourseNotification(Guid accountID, int page, int numberOfItemPerPage, ref int totalItems)
        {
            using (DLULNEntities db = new DLULNEntities())
            {
                IQueryable<CourseNotification> query = db.CourseNotifications
                       .Include("Feed").Include("Feed.Account")
                        .Include("Feed.Account1").Include("Feed.FeedComment.Feed")
                        .Include("Feed.FeedComment.Feed.Account")
                        .Include("Feed.FeedComment.Feed.Account1")
                        .Include("Feed.FeedComment.Feed.Course")
                        .Include("Feed.Course")
                        .Where(cNotification => cNotification.AccountID_u == accountID);
                return GetPager(query, page, numberOfItemPerPage, ref totalItems);
            }
        }

        public static int UpdateReadState(Guid courseNotificationID, Guid accountID)
        {
            try
            {
                using (DLULNEntities db = new DLULNEntities())
                {
                    CourseNotification courseNotification = db.CourseNotifications.SingleOrDefault(fn => fn.CourseNotificationID_u == courseNotificationID);
                    if (courseNotification != null && courseNotification.AccountID_u == accountID)
                    {
                        courseNotification.IsRead_b = true;
                        if (db.SaveChanges() > 0)
                        {
                            return 1;
                        }
                    }
                    return 0;
                }
            }
            catch
            {
                return 101;
            }
        }

        public static int Delete(Guid courseNotificationID, Guid accountID)
        {
            try
            {
                using (DLULNEntities db = new DLULNEntities())
                {
                    CourseNotification courseNotification = db.CourseNotifications.SingleOrDefault(fn => fn.CourseNotificationID_u == courseNotificationID);
                    if (courseNotification != null && courseNotification.AccountID_u == accountID)
                    {
                        db.CourseNotifications.DeleteObject(courseNotification);
                        if (db.SaveChanges() > 0)
                        {
                            return 1;
                        }
                    }
                    return 0;
                }
            }
            catch
            {
                return 101;
            }
        }

        public static List<CourseNotification> GetLatestCourseNotification(Guid accountID, DateTime time)
        {
            try
            {
                using (DLULNEntities db = new DLULNEntities())
                {
                    return db.CourseNotifications.Include("Feed").Include("Feed.Account")
                        .Include("Feed.Account1").Include("Feed.FeedComment.Feed")
                        .Include("Feed.FeedComment.Feed.Account")
                        .Include("Feed.FeedComment.Feed.Account1")
                        .Include("Feed.FeedComment.Feed.Course")
                        .Include("Feed.Course").OrderBy(cn=>cn.Feed.CreateDate_dt)
                        .Where(cNotification => cNotification.AccountID_u == accountID && cNotification.IsRead_b == false && cNotification.Feed.CreateDate_dt.CompareTo(time) > 0).ToList();
                }
            }
            catch
            {
                return null;
            }
        }
    }
}
